package org.example.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import org.example.R.R;
import org.example.entity.dto.AddNoticeDto;
import org.example.entity.po.NoticePO;
import org.example.entity.vo.NoticeVO;
import org.example.entity.vo.OSSFileVO;
import org.example.entity.vo.pc.PCNoticeVO;
import org.example.mapper.NoticeMapper;
import org.example.mapper.OSSMapper;
import org.example.mapper.PartyBuildingGuidanceMapper;
import org.example.utils.TimeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    @Autowired
    private OSSMapper ossMapper;


    @Autowired
    private PartyBuildingGuidanceMapper partyBuildingGuidanceMapper;

    public R add(AddNoticeDto dto) {
        String msg = "";
        if (dto.getId() == null) {
            NoticePO po = new NoticePO();
            BeanUtils.copyProperties(dto, po);
            po.setDelFlag(0);
            po.setId(IdUtil.getSnowflakeNextId());

            po.setCreateTime(TimeUtils.getCurrentDate());
            noticeMapper.insert(po);
            msg = "发布成功!";
        } else {
            NoticeVO noticeVO = noticeMapper.findOne(dto.getId());
            if (noticeVO == null) {
                return R.error("该通知不存在!");
            }
            noticeMapper.deleteNotice(dto.getId());
            NoticePO po = new NoticePO();
            BeanUtils.copyProperties(dto, po);
            po.setId(IdUtil.getSnowflakeNextId());

            po.setDelFlag(0);
            po.setCreateTime(TimeUtils.getCurrentDate());
            noticeMapper.insert(po);
            msg = "编辑成功!";
        }
        return R.success(msg);
    }

    public R delete(Long id) {
        noticeMapper.deleteNotice(id);
        return R.success("删除成功!");
    }

    public R list() {
        List<NoticeVO> noticeVOList = noticeMapper.listNotice();
        List<Long> resourceIds = noticeVOList.stream().map(NoticeVO::getResourceId).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(resourceIds)) {
            List<OSSFileVO> ossFileVOS = ossMapper.selectOssNameFromIds(resourceIds);
            for (NoticeVO noticeVO : noticeVOList) {
                for (OSSFileVO ossFileVO : ossFileVOS) {
                    if (noticeVO.getResourceId() != null && noticeVO.getResourceId().equals(ossFileVO.getId())) {
                        noticeVO.setResourceName(ossFileVO.getOssFileName());
                        noticeVO.setResourceUrl(ossFileVO.getOssFileUrl());
                    }
                }
            }
        }
        noticeVOList.sort(Comparator.comparing(NoticeVO::getCreateTime).reversed());
        return R.success("查询成功!", noticeVOList);
    }

    public R pcNoticeList() {
        PCNoticeVO pcNoticeVO = new PCNoticeVO();


        List<NoticeVO> noticeVOList = noticeMapper.listNotice();
        List<Long> resourceIds = noticeVOList.stream().map(NoticeVO::getResourceId).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(resourceIds)) {
            List<OSSFileVO> ossFileVOS = ossMapper.selectOssNameFromIds(resourceIds);
            for (NoticeVO noticeVO : noticeVOList) {
                for (OSSFileVO ossFileVO : ossFileVOS) {
                    if (noticeVO.getResourceId() != null && noticeVO.getResourceId().equals(ossFileVO.getId())) {
                        noticeVO.setResourceName(ossFileVO.getOssFileName());
                    }
                }
            }
        }
        noticeVOList.sort(Comparator.comparing(NoticeVO::getCreateTime));

        List<List<String>> result = new ArrayList<>();
        List<List<String>> clickResult = new ArrayList<>();
        noticeVOList.forEach(obj -> {
            List<String> vo = new ArrayList<>();
            List<String> clickVo = new ArrayList<>();
            vo.add(String.valueOf(obj.getId()));
            vo.add(obj.getTitle());
            vo.add(obj.getCreateTime());

            clickVo.add(String.valueOf(obj.getId()));
            clickVo.add(obj.getTitle());
            clickVo.add(obj.getCreateTime());

            result.add(vo);
            clickResult.add(clickVo);
        });

        pcNoticeVO.setShowData(result);
        pcNoticeVO.setClickData(clickResult);

        return R.success("查询成功!", pcNoticeVO);
    }

    public R pcNoticeDetail(Long id) {
        NoticeVO one = noticeMapper.findOne(id);
        if (one == null) {
            return R.error("该公告不存在!");
        }
        if (one.getResourceId() != null) {
            String ossFileUrl = "";
            String ossFileName = "";
            List<OSSFileVO> ossFileVOS = ossMapper.selectOssNameFromIds(Arrays.asList(one.getResourceId()));
            if (CollectionUtil.isNotEmpty(ossFileVOS)) {
                ossFileUrl = ossFileVOS.get(0).getOssFileUrl();
                ossFileName = ossFileVOS.get(0).getOssFileName();
                one.setResourceUrl(ossFileUrl);
                one.setResourceName(ossFileName);
            }
        }

        return R.success("获取成功").setData(one);
    }

    public R userListImport() {
        PCNoticeVO pcNoticeVO = new PCNoticeVO();
        List<List<String>> result = new ArrayList<>();


        List<Map<String, Object>> tableData = partyBuildingGuidanceMapper.selectTableData("T_RYXXK");
        Set<Map<String, Object>> sortedData = new HashSet<>();

        // 遍历原始列表，找到包含id的第一个Map并添加到新列表的开头
        for (Map<String, Object> row : tableData) {
            if (row.containsKey("id")) {
                sortedData.add(row);
                break;
            }
        }
        // 将原始列表中剩余的Map添加到新列表的末尾
        sortedData.addAll(tableData);

        for (Map<String, Object> datum : sortedData) {
            List<String> list = new ArrayList<>();
            for (Map.Entry<String, Object> entry : datum.entrySet()) {
                String value = (String) entry.getValue();
                list.add(value);
            }
            result.add(list);
        }
        pcNoticeVO.setShowData(result);
        return R.success("查询成功!", pcNoticeVO);
    }

    public R dwJwListImport() {
        PCNoticeVO pcNoticeVO = new PCNoticeVO();
        List<List<String>> result = new ArrayList<>();


        List<Map<String, Object>> tableData = partyBuildingGuidanceMapper.selectTableData("T_DJ_JFQK");
        Set<Map<String, Object>> sortedData = new HashSet<>();

        // 遍历原始列表，找到包含id的第一个Map并添加到新列表的开头
        for (Map<String, Object> row : tableData) {
            if (row.containsKey("id")) {
                sortedData.add(row);
                break;
            }
        }
        // 将原始列表中剩余的Map添加到新列表的末尾
        sortedData.addAll(tableData);

        for (Map<String, Object> datum : sortedData) {
            List<String> list = new ArrayList<>();
            for (Map.Entry<String, Object> entry : datum.entrySet()) {
                String value = (String) entry.getValue();
                list.add(value);
            }
            result.add(list);
        }
        pcNoticeVO.setShowData(result);
        return R.success("查询成功!", pcNoticeVO);
    }
}
